package com.szd.qiongyou20.home.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.szd.qiongyou20.R;
import com.szd.qiongyou20.activity.CityActivity;
import com.szd.qiongyou20.home.activity.OnlyWebViewActivity;
import com.szd.qiongyou20.home.activity.ShowActivity;
import com.szd.qiongyou20.home.activity.ShowOverTopActivity;
import com.szd.qiongyou20.home.adapter.RecyclerView_Home_Adapter;
import com.szd.qiongyou20.home.adapter.ViewPager_Home_Adapter;
import com.szd.qiongyou20.home.bean.HomeListData;
import com.szd.qiongyou20.home.bean.HomeNearData;
import com.szd.qiongyou20.decoration.RecyclerItemClickListener;
import com.szd.qiongyou20.decoration.SpacesItemDecoration;
import com.szd.qiongyou20.home.presenter.HomeListPre;
import com.szd.qiongyou20.home.view.HomeDataToView;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by SZD on 2016/11/25.
 */

public class Fragment_Home extends Fragment implements HomeDataToView {

    @Bind(R.id.viewPager_home)
    ViewPager mViewPagerHome;
    @Bind(R.id.imageView_comments)
    ImageView mImageViewComments;
    @Bind(R.id.textView_content)
    TextView mTextViewContent;
    @Bind(R.id.textView_type)
    TextView mTextViewType;
    @Bind(R.id.textView_city0)
    TextView mTextViewCity0;
    @Bind(R.id.layout_city0)
    RelativeLayout mLayoutCity0;
    @Bind(R.id.imageView_home_jdwl)
    ImageView mImageViewHomeJdwl;
    @Bind(R.id.textView_home_jdwl)
    TextView mTextViewHomeJdwl;
    @Bind(R.id.imageView_home_foods)
    ImageView mImageViewHomeFoods;
    @Bind(R.id.textView_home_foods)
    TextView mTextViewHomeFoods;
    @Bind(R.id.imageView_home_jn)
    ImageView mImageViewHomeJn;
    @Bind(R.id.textView_home_jn)
    TextView mTextViewHomeJn;
    @Bind(R.id.imageView_home_enshrine)
    ImageView mImageViewHomeEnshrine;
    @Bind(R.id.textView_home_enshrine)
    TextView mTextViewHomeEnshrine;
    @Bind(R.id.textView_item_city1)
    TextView mTextViewItemCity1;
    @Bind(R.id.layout_city1)
    LinearLayout mLayoutCity1;
    @Bind(R.id.textView_item_city2)
    TextView mTextViewItemCity2;
    @Bind(R.id.layout_city2)
    LinearLayout mLayoutCity2;
    @Bind(R.id.textView_item_city3)
    TextView mTextViewItemCity3;
    @Bind(R.id.layout_city3)
    LinearLayout mLayoutCity3;
    @Bind(R.id.recyclerView_home)
    RecyclerView mRecyclerViewHome;
    @Bind(R.id.imageView_city0)
    ImageView mImageViewCity0;
    @Bind(R.id.radioGroup_viewPager)
    RadioGroup mRadioGroupViewPager;

    private Context mContext;
    private RadioButton[] mRadioButtons;
    private Handler mHandler;
    private int index;
    Timer timer = new Timer();

    private HomeListPre mHomeListPre;

    public Fragment_Home() {

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
        mHomeListPre = new HomeListPre(this);
        mHomeListPre.loadDataToView(mContext);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.bind(this, view);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mRecyclerViewHome.setLayoutManager(new LinearLayoutManager(mContext));
        mRecyclerViewHome.addItemDecoration(new SpacesItemDecoration(20));
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }


    /**
     * 添加顶部的ViewPager的适配器
     *
     * @param slideBean
     */
    @Override
    public void toViewPager(final List<HomeListData.DataBean.SlideBean> slideBean) {

        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                mViewPagerHome.setCurrentItem(msg.arg1);
            }
        };

        ViewPager_Home_Adapter adapter = new ViewPager_Home_Adapter(mContext, slideBean);
        mViewPagerHome.setAdapter(adapter);

        mViewPagerHome.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(final int position, float positionOffset, int positionOffsetPixels) {
                mRadioButtons[position%slideBean.size()].setChecked(true);
            }

            @Override
            public void onPageSelected(final int position) {
                index = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                Message msg = new Message();
                msg.arg1 = index + 1;
                mHandler.sendMessage(msg);
            }
        }, 3000, 3000);


        // 设置滚动的dots
        mRadioButtons = new RadioButton[slideBean.size()];
        for (int i = 0; i < slideBean.size(); i++) {
            mRadioButtons[i] = new RadioButton(mContext);
            mRadioButtons[i].setButtonDrawable(R.drawable.radio_button);
            mRadioGroupViewPager.addView(mRadioButtons[i]);
        }
        mRadioButtons[0].setChecked(true);
    }

    /**
     * 添加底部RecyclerView的适配器
     *
     * @param entryBeen
     */
    @Override
    public void toRecyclerView(final List<HomeListData.DataBean.FeedBean.EntryBean> entryBeen) {
        RecyclerView_Home_Adapter adapter = new RecyclerView_Home_Adapter(mContext, entryBeen);
        mRecyclerViewHome.setAdapter(adapter);

        mRecyclerViewHome.addOnItemTouchListener(new RecyclerItemClickListener(mContext,
                new RecyclerItemClickListener.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                String url = entryBeen.get(position).getUrl();
                Intent intent = new Intent();
                intent.putExtra("url", url);
                if (url.endsWith("html")) {
                    intent.setClass(mContext, ShowOverTopActivity.class);
                } else {
                    intent.setClass(mContext, ShowActivity.class);
                }
                mContext.startActivity(intent);
            }

            @Override
            public void onLongClick(View view, int position) {

            }
        }));
    }

    /**
     * 给中间的评价条目添加数据
     *
     * @param data
     */
    @Override
    public void toComment_entry(HomeListData.DataBean.CommentEntryBean data) {
        // 将值赋给条目中的控件
        Glide.with(mContext).load(data.getIcon_url()).into(mImageViewComments);
        mTextViewContent.setText(data.getTitle());

        final String url = data.getUrl();
        mTextViewContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, OnlyWebViewActivity.class);
                intent.putExtra("url", url);
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public void toNear(HomeNearData.DataBean dataBean) {
        // 获取城市的id用来处理点击事件时的拼接
        final int city_id = dataBean.getCity_id();
        String title = dataBean.getTitle();
        String city_name = dataBean.getCity_name();
        String cover = dataBean.getCover();
        mTextViewType.setText(title);
        mTextViewCity0.setText(city_name);
        Glide.with(mContext).load(cover).into(mImageViewCity0);

        mImageViewCity0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, CityActivity.class);
                intent.putExtra("city_id", city_id);
                mContext.startActivity(intent);
            }
        });

        // 获取四个分类的数据
        List<HomeNearData.DataBean.TabBean> tab = dataBean.getTab();
        mTextViewHomeJdwl.setText(tab.get(0).getName());
        Glide.with(mContext).load(tab.get(0).getIcon_url()).into(mImageViewHomeJdwl);
        mTextViewHomeFoods.setText(tab.get(1).getName());
        Glide.with(mContext).load(tab.get(1).getIcon_url()).into(mImageViewHomeFoods);
        mTextViewHomeJn.setText(tab.get(2).getName());
        Glide.with(mContext).load(tab.get(2).getIcon_url()).into(mImageViewHomeJn);
        mTextViewHomeEnshrine.setText(tab.get(3).getName());
        Glide.with(mContext).load(tab.get(3).getIcon_url()).into(mImageViewHomeEnshrine);

        // 获取三个推荐的城市
        List<HomeNearData.DataBean.RecommendCityBean> recommend_city =
                dataBean.getRecommend_city();
        mTextViewItemCity1.setText(recommend_city.get(0).getCity_name());
        mTextViewItemCity2.setText(recommend_city.get(1).getCity_name());
        mTextViewItemCity3.setText(recommend_city.get(2).getCity_name());
    }
}
